function [shapeT R s T] = alignShapePair(x2, x1)
%sR is the rotation mulitply scale
%align shape with shapeT
[sy sx] = size(x2);
%unit weight
w = 1/sx;

%1 is the target
%X1, Y1, X2, Y2,C2, C1, Z, W=1
X1 = sum(x1(1,:)*w);
Y1 = sum(x1(2,:)*w);

X2 = sum(x2(1,:)*w);
Y2 = sum(x2(2,:)*w);

Z = sum((x2(1,:).^2 + x2(2,:).^2)*w);

C1 = sum((x1(1,:).*x2(1,:) + x1(2,:).*x2(2,:))*w);
C2 = sum((x1(2,:).*x2(1,:) - x1(1,:).*x2(2,:))*w);

W = 1;

A = [ X2  -Y2 W  0;
      Y2  X2  0  W;
      Z   0   X2 Y2;
      0   Z  -Y2 X2];
b = [X1 Y1 C1 C2]';

X = (A\b)';

sR = [X(1) -X(2);
      X(2)  X(1)];
  
s = norm(sR);
R = sR./s;  
T = [X(3) X(4)]';

shapeT = 1/s*inv(R)*(x1 - repmat(T,1,sx));
%shapeT = R'*1/s(ShapeT - repmat(T,1,sx));

  
